package cn.com.open.itembank.controller;

import cn.com.open.itembank.common.GlobalConst;
import cn.com.open.itembank.dto.IdentifierArray;
import cn.com.open.itembank.dto.QuestionModel;
import cn.com.open.itembank.dto.ReturnModel;
import cn.com.open.itembank.interceptor.Log;
import cn.com.open.itembank.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;

/**
 * Created by lucky on 2017/7/13.
 */

/**
 * 大题控制器
 */
@RestController
@RequestMapping("/itembank")
public class SectionController extends BaseController {
    private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(SectionController.class);

    @Autowired
    private ItemBankTestPaperRemoteService testPaperService;

    //region 新增相关（Create：理论方法 HttpPost）

    //region 新增大题试题

    /**
     * 新增大题试题
     * 历史接口：api/itemservice4/itembank/{itembankId}/paper/{paperId}/addnewquestion
     *
     * @param request       the request
     * @param response      the response
     * @param itembankId    题库ID
     * @param paperId       试卷ID
     * @param questionModel 试题模型
     * @param shuf          是否乱序
     * @param score         分数
     */
    @Log(optType = "新增大题试题", optDescription = "新增大题试题，参数：%s", keyName = "questionModel")
    @PostMapping(value = "{itembankId}/paper/{paperId}/addnewquestion")
    public void addNewQuestion(HttpServletRequest request, HttpServletResponse response,
                               @PathVariable(value = "itembankId") String itembankId,
                               @PathVariable(value = "paperId") String paperId,
                               @RequestBody QuestionModel questionModel,
                               @RequestParam("shuf") boolean shuf,
                               @RequestParam("score") double score) {

        String appKey = request.getHeader(appKeyString);
        ReturnModel model = testPaperService.addNewQuestion(itembankId, paperId, questionModel, shuf, score, appKey);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 新增大题试题(关联)

    /**
     * 新增大题试题(关联)
     * 历史接口：openapi/v3/Paper/{pid}/AddExistQuestion
     *
     * @param request    the request
     * @param response   the response
     * @param itembankId 题库ID
     * @param paperId    试卷ID
     * @param questionId 试题id
     * @param shuf       是否乱序
     * @param score      分数
     */
    @Log(optType = "新增大题试题(关联)", optDescription = "新增大题试题(关联)，ID：%s", keyName = "questionId")
    @PostMapping(value = "{itembankId}/paper/{paperId}/question/{questionId}")
    public void addExistQuestion(HttpServletRequest request, HttpServletResponse response,
                                 @PathVariable(value = "itembankId") String itembankId,
                                 @PathVariable(value = "paperId") String paperId,
                                 @PathVariable(value = "questionId") String questionId,
                                 @RequestParam("shuf") boolean shuf,
                                 @RequestParam("s") double score) {

        String appKey = request.getHeader(appKeyString);
        ReturnModel model = testPaperService.addSectionQuestion(itembankId, paperId, questionId, shuf, score, appKey);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 批量新增大题试题(关联)

    /**
     * 批量新增大题试题(关联)
     * 历史接口：openapi/v3/Paper/{pid}/AddExistQuestion
     *
     * @param request         the request
     * @param response        the response
     * @param itembankId      题库ID
     * @param paperId         试卷ID
     * @param identifierArray 试题id集合
     * @param score           分数
     */
    @Log(optType = "批量新增大题试题(关联)", optDescription = "批量新增大题试题(关联)，IDs:%s", keyName = "identifierArray")
    @PostMapping(value = "{itembankId}/paper/{paperId}/addexistquestionlist")
    public void addExistQuestionList(HttpServletRequest request, HttpServletResponse response,
                                     @PathVariable(value = "itembankId") String itembankId,
                                     @PathVariable(value = "paperId") String paperId,
                                     @RequestBody IdentifierArray identifierArray,
                                     @RequestParam("score") double score) {

        String appKey = request.getHeader(appKeyString);
        ReturnModel model = testPaperService.addExistQuestionList(itembankId, paperId, identifierArray.getIdList(), score, appKey);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //endregion

    //region 删除相关（Delete：理论方法 HttpDelete）

    //region 删除大题

    /**
     * 大题自删除大题身删除
     *
     * @param request    equest
     * @param response   the response
     * @param itembankId the item bank id
     * @param paperId    the paperId
     * @param sectionId  the sectionId
     * @return
     */
    @Log(optType = "删除大题", optDescription = "删除大题，ID：%s", keyName = "sectionId")
    @DeleteMapping(value = "/{itembankId}/paper/{paperId}/section/{sectionId}")
    public void removeSection(HttpServletRequest request, HttpServletResponse response,
                              @PathVariable(value = "itembankId") String itembankId,
                              @PathVariable(value = "paperId") String paperId,
                              @PathVariable("sectionId") String sectionId) {

        String appkey = request.getHeader(appKeyString);
        ReturnModel model = testPaperService.removeSection(itembankId, paperId, sectionId, appkey);

        if (model.getCode() == GlobalConst.SUCCESS) {
            response.setStatus(HttpServletResponse.SC_NO_CONTENT);
        } else if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 删除大题试题

    /**
     * 删除大题试题
     *
     * @param request    the request
     * @param response   the response
     * @param itembankId the item bank id
     * @param paperId    the paperId
     * @param sectionId  the sectionId
     * @param questionId the question id
     * @return
     */
    @Log(optType = "删除大题试题", optDescription = "删除大题试题，ID：%s", keyName = "questionId")
    @DeleteMapping(value = "/{itembankId}/paper/{paperId}/section/{sectionId}/question/{questionId}")
    public void removeSectionQuestion(HttpServletRequest request, HttpServletResponse response,
                                      @PathVariable(value = "itembankId") String itembankId,
                                      @PathVariable(value = "paperId") String paperId,
                                      @PathVariable("sectionId") String sectionId,
                                      @PathVariable("questionId") String questionId) {

        String appKey = request.getHeader(appKeyString);
        ReturnModel model = testPaperService.removeSectionQuestion(itembankId, paperId, sectionId, questionId, appKey);

        if (model.getCode() == GlobalConst.SUCCESS) {
            response.setStatus(HttpServletResponse.SC_NO_CONTENT);
        } else if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 批量删除大题试题

    /**
     * 批量删除大题试题
     *
     * @param request    the request
     * @param response   the response
     * @param itembankId the item bank id
     * @param paperId    the paperId
     * @param idList     the question id
     * @return
     */
    @Log(optType = "批量删除大题试题", optDescription = "批量删除大题试题，IDs：%s", keyName = "idList")
    @DeleteMapping(value = "/{itembankId}/paper/{paperId}/question")
    public void removeSectionQuestionList(HttpServletRequest request, HttpServletResponse response,
                                          @PathVariable(value = "itembankId") String itembankId,
                                          @PathVariable(value = "paperId") String paperId,
                                          @RequestBody IdentifierArray idList) {

        String appKey = request.getHeader(appKeyString);
        ReturnModel model = testPaperService.removeSectionQuestionList(itembankId, paperId, idList, appKey);

        if (model.getCode() == GlobalConst.SUCCESS) {
            response.setStatus(HttpServletResponse.SC_NO_CONTENT);
        } else if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //endregion

    //region 更新相关（Update：理论方法 HttpPut）

    //region 修改大题

    /**
     * 修改大题
     *
     * @param request      the request
     * @param response     the response
     * @param itembankId   题库id
     * @param paperId      试卷ID
     * @param sectionId    大题ID
     * @param sectionTitle 大题标题
     * @param shuf         是否乱序
     * @return
     */
    @Log(optType = "修改大题", optDescription = "修改大题，ID：%s", keyName = "sectionId")
    @PutMapping(value = "/{itembankId}/paper/{paperId}/section/{sectionId}")
    public void updateSection(HttpServletRequest request, HttpServletResponse response,
                              @PathVariable(value = "itembankId") String itembankId,
                              @PathVariable(value = "paperId") String paperId,
                              @PathVariable("sectionId") String sectionId,
                              @RequestParam("c") String sectionTitle,
                              @RequestParam("shuf") boolean shuf) {

        String appKey = request.getHeader(appKeyString);
        ReturnModel model = testPaperService.updateSectionContent(itembankId, paperId, sectionId, sectionTitle, shuf, appKey);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 修改大题试题分值

    /**
     * 大题试题分值修改
     *
     * @param request    the request
     * @param response   the response
     * @param itembankId 题库id
     * @param paperId    试卷ID
     * @param sectionId  大题ID
     * @param questionId 试题id
     * @param score      试题分值
     * @return
     */
    @Log(optType = "大题试题分值修改", optDescription = "大题试题分值修改，ID：%s", keyName = "questionId")
    @PutMapping(value = "/{itembankId}/paper/{paperId}/section/{sectionId}/question/{questionId}/score")
    public void updateSectionQuestionScore(HttpServletRequest request, HttpServletResponse response,
                                           @PathVariable(value = "itembankId") String itembankId,
                                           @PathVariable(value = "paperId") String paperId,
                                           @PathVariable("sectionId") String sectionId,
                                           @PathVariable("questionId") String questionId,
                                           @RequestParam("s") double score) {

        String appKey = request.getHeader(appKeyString);
        ReturnModel model = testPaperService.updateSectionQuestionScore(itembankId, paperId, sectionId, questionId, score, appKey);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 修改大题所有试题分值

    /**
     * 大题所有试题分值修改
     *
     * @param request    the request
     * @param response   the response
     * @param itembankId 题库id
     * @param paperId    试卷ID
     * @param sectionId  大题ID
     * @param score      试题分值
     * @return
     */
    @Log(optType = "大题所有试题分值修改", optDescription = "大题所有试题分值修改，ID：%s", keyName = "sectionId")
    @PutMapping(value = "/{itembankId}/paper/{paperId}/section/{sectionId}/score")
    public void updateSectionScore(HttpServletRequest request, HttpServletResponse response,
                                   @PathVariable(value = "itembankId") String itembankId,
                                   @PathVariable(value = "paperId") String paperId,
                                   @PathVariable("sectionId") String sectionId,
                                   @RequestParam("s") double score) {

        String appKey = request.getHeader(appKeyString);
        ReturnModel model = testPaperService.updateSectionScore(itembankId, paperId, sectionId, score, appKey);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 修改大题试题

    /**
     * Update section question.
     *
     * @param request       the request
     * @param response      the response
     * @param itembankId    题库ID
     * @param paperId       试卷ID
     * @param sectionId     大题ID
     * @param questionId    the question id
     * @param copy          是否复制
     * @param questionModel 试题模型
     * @return
     */
    @Log(optType = "大题试题修改", optDescription = "大题试题修改，ID：%s", keyName = "questionId")
    @PutMapping(value = "/{itembankId}/paper/{paperId}/section/{sectionId}/question/{questionId}/edit")
    public void updateSectionQuestion(HttpServletRequest request, HttpServletResponse response,
                                      @PathVariable(value = "itembankId") String itembankId,
                                      @PathVariable(value = "paperId") String paperId,
                                      @PathVariable(value = "sectionId") String sectionId,
                                      @RequestParam(value = "copy", required = false, defaultValue = "false") Boolean copy,
                                      @PathVariable(value = "questionId") String questionId,
                                      @RequestBody QuestionModel questionModel) {

        String appKey = request.getHeader(appKeyString);
        ReturnModel model = testPaperService.updateSectionQuestion(itembankId, paperId, sectionId, questionId, copy, questionModel, appKey);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 调整大题顺序

    /**
     * 调整大题顺序
     *
     * @param request    the request
     * @param response   the response
     * @param itembankId 题库id
     * @param itembankId 试卷ID
     * @param sectionId  大题ID
     * @param m          上移/下移
     * @return
     */
    @Log(optType = "大题顺序调整", optDescription = "大题顺序调整，ID：%s", keyName = "sectionId")
    @PutMapping(value = "/{itembankId}/paper/{paperId}/section/{sectionId}/movement")
    public void updateSectionOrder(HttpServletRequest request, HttpServletResponse response,
                                   @PathVariable(value = "itembankId") String itembankId,
                                   @PathVariable(value = "paperId") String paperId,
                                   @PathVariable("sectionId") String sectionId,
                                   @RequestParam("m") boolean m) {

        String appKey = request.getHeader(appKeyString);
        ReturnModel model = testPaperService.updateSectionOrder(itembankId, paperId, sectionId, m, appKey);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 调整大题试题顺序

    /**
     * 调整大题试题顺序
     *
     * @param request    the request
     * @param response   the response
     * @param itembankId 题库id
     * @param paperId    试卷ID
     * @param sectionId  大题ID
     * @param questionId the question id
     * @param m          上移/下移
     * @return return model
     */
    @Log(optType = "调整大题试题顺序", optDescription = "调整大题试题顺序，ID：%s", keyName = "questionId")
    @PutMapping(value = "/{itembankId}/paper/{paperId}/section/{sectionId}/questionId/{questionId}/movement")
    public void updateSectionQuestionOrder(HttpServletRequest request, HttpServletResponse response,
                                           @PathVariable(value = "itembankId") String itembankId,
                                           @PathVariable(value = "paperId") String paperId,
                                           @PathVariable("sectionId") String sectionId,
                                           @PathVariable("questionId") String questionId,
                                           @RequestParam("m") boolean m) {

        String appKey = request.getHeader(appKeyString);
        ReturnModel model = testPaperService.updateSectionQuestionOrder(itembankId, paperId, sectionId, questionId, m, appKey);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //endregion

    //region 查询相关（Retrieve：理论方法 HttpGet）

    //endregion
}
